<template>
  <div class="ChildPage TablePage">
    <vb-page ref="page">
      <template #table-stockNo="scope">
        {{ scope.row.stockNo? scope.row.stockNo + ":" + getStockName(scope.row.stockNo): scope.row.stockNo }}
      </template>
      <template #table-toStockNo="scope">
        {{ scope.row.toStockNo? scope.row.toStockNo + ":" + getStockName(scope.row.toStockNo): scope.row.toStockNo }}
      </template>
      <template #table-status="scope">
        <el-tag :type="scope.row.status ==='save'? 'info': 'success'">{{ scope.row.status ==='save'? "保存": "审核" }}</el-tag>
      </template>
      <template #table-allocationType="scope">
        {{ getAllocationTypeName(scope.row.allocationType) }}
      </template>
      <template #table-handle-edit="scope">
        <el-button type="text" :icon="scope.row.status === 'save'? 'el-icon-edit': 'el-icon-view'" @click="gotoDetail(scope.row.billNo)">{{ scope.row.status === 'save'? '编辑': '查看' }}</el-button>
      </template>
    </vb-page>
  </div>
</template>

<script>
export default {
  data() {
    return{
      loading: false,
      stockList:[],
      allocationTypeList: []
    }
  },
  async mounted() {
    await this.getStock()
  },
  methods:{
    async getStock() {
      this.loading = true;
      try {
        let res = await this.$api.getChildDictionaryListByKeys({
          keys: ['stock', 'allocationType']
        });
        if (!res.res) return;
        this.stockList = res.stock;
        this.allocationTypeList = res.allocationType;
      } finally {
        this.loading = false;
      }
    },
    /**
     * 获取仓库名
     */
    getStockName(val) {
      if (this.stockList && this.stockList.length > 0) {
        for(let i = 0; i < this.stockList.length; i++) {
          if (this.stockList[i].key === val) {
            return this.stockList[i].name;
          }
        }
      }
      return '';
    },
    /**
     * 获取仓库名
     */
    getAllocationTypeName(val) {
      if (this.allocationTypeList && this.allocationTypeList.length > 0) {
        for(let i = 0; i < this.allocationTypeList.length; i++) {
          if (this.allocationTypeList[i].key === val) {
            return this.allocationTypeList[i].name;
          }
        }
      }
      return '';
    },
    /**
     * 跳转盘点方案详细
     */
    gotoDetail(billNo = '') {
      let url = '/allocation/allocationHistoryDetail'
      if (billNo) {
        url += `?billNo=${ billNo }`
      }
      this.$router.push(url)
    },
    async doDelete(row) {
      try {
        await this.$confirm(`确定删除单据【${ row.billNo }】吗`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
      } catch(e) {
        return
      }
      this.loading = true
      try {
        let res = await this.callApi('delete_stock_take', { billNo: row.billNo })
        if (!res.res) {
          return
        }
        this.$message.success(`已删除`)
        this.$refs.page.reload()
      } finally {
        this.loading = false
      }
    },
    async doTask(row) {
      try {
        await this.$confirm(`确定下发吗？`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
      } catch(e) {
        return
      }
      this.loading = true
      try {
        let res = await this.callApi('to_stock_take_task', { billNo: row.billNo })
        if (!res.res) {
          return
        }
        this.$message.success(`已下发!`)
      } finally {
        this.loading = false
      }
    },
  }
}
</script>

<style scoped>

</style>
